package ru.integrationmessagers.vk.client.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import ru.integrationmessagers.vk.client.VkClient;
import ru.integrationmessagers.vk.config.properties.VkProperty;
import ru.integrationmessagers.vk.model.config.vk.methods.VkFriendMethods;
import ru.integrationmessagers.vk.model.config.vk.paths.VkPaths;
import ru.integrationmessagers.vk.model.domain.Friend;
import ru.integrationmessagers.vk.model.dto.vk.VkFriendDto;
import ru.integrationmessagers.vk.model.response.vk.VkResponse;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.vk.api.sdk.objects.users.Fields.PHOTO_200_ORIG;

@Component
@RequiredArgsConstructor
public class VkClientImpl implements VkClient {

    private final RestTemplate restTemplate;
    private final VkProperty vkProperty;

    @Override
    public List<VkFriendDto> getFriends() {
        VkResponse response = restTemplate.getForObject(
                VkPaths.FRIENDS.getPath() + VkFriendMethods.GET_ALL.getMethod() + "?access_token={access_token_value}&v={v_value}&user_id={user_id_value}&fields={fields_value}",
                VkResponse.class,
                createParams()
        );
        return response.getResponse().getItems();

    }

    private Map<String, String> createParams() {
        Map<String, String> params = new HashMap<>();

        params.put("access_token_value", vkProperty.getAccessToken());
        params.put("v_value", vkProperty.getVersion());
        params.put("user_id_value", vkProperty.getUserId());
        params.put("fields_value", PHOTO_200_ORIG.getValue());

        return params;
    }

}
